<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>注册</title>
    <link rel="stylesheet" type="text/css" href="../css/common.css" />
    <style>
          .row {
            box-sizing: border-box;
            width: auto;
            height: 50px;
            margin-left: 32px;
            margin-right: 32px;
            padding-top: 10px;
            border-bottom: 1px solid #ccc;
        }

        .input {
            width: 100%;
            height: 50px;
            line-height: 50px;
            border: none;
            outline: none;
            font-size: 16px;
        }

        .btn {
            width: auto;
            height: 50px;
            margin: 30px 100px;

            line-height: 50px;

            font-size: 24px;
            text-align: center;
            border-radius: 8px;
        }

        .highlight {
            opacity: 0.7;
        }

        .reglogo img {
          
            height: 100px;
        }
        .check-code {
            display: flex;
            flex-direction: row;
            justify-content: space-between;
        }
    </style>
</head>

<body>
    <div class="reglogo  " style="height: 100px; margin-top: 30px; text-align: center;">
        <img src="../images/logo.png" />
    </div>
    <div class="row  ">
        <input id="userphone" class="input" type="text" placeholder="手机号">
    </div>
    <div class="row  ">
        <input id="username" class="input" type="text" placeholder="用户名">
    </div>
    <div class="row check-code" >
        <input style="width:45%;" id="code" class="input" type="text" placeholder="验证码">
        <div id="codebtn" class=" textac Cwhite backCBlue br2 w100px"
         style="height: 30px;line-height:30px;margin-top: -2px;" tapmode="highlight"
            onclick="getCode();">获取验证码</div>
    </div>
    <div class="row">
        <input id="password" class="input" type="password" placeholder="密码">
    </div>
    <div class="row">
        <input id="password2" class="input" type="password" placeholder="确认密码">
    </div>

    <div class="row" >
        <input id="invite" class="input" type="text" placeholder="邀请码">

    </div>
    <div class="row login-info" style="border-bottom:0px ;">
        <input type="checkbox" id="agree" />
        <div>
            我已经阅并同意本APP的<span  class="key-link" onclick="core.news(2)">《服务协议》</span>和<span  class="key-link"
            onclick="core.news(1)">《用户隐私协议》</span>。
        </div>
    </div>
    <div class="btn  btn-orange" tapmode="highlight" onclick="fnRegister();">注册</div>

    <div style="margin-top: 50px; text-align: center;" tapmode="highlight" onclick="core.back();">取消注册</div>

</body>
<script type="text/javascript" src="../script/api.js"></script>
<script src="../script/common.js"></script>

<script type="text/javascript">

    let timer = setInterval(() => {
        if (timestart) {
            timec--;
            console.log("每隔一秒我将输出一次", timec)
            var dom = document.getElementById("codebtn");
            dom.innerHTML = timec + "秒";
            if (timec < 0) {
                timestart = false;
                dom.innerHTML = "发送验证码";
            }
        }
    }, 1000)
    var htmldata = {
        getcode: "",
    }
    apiready = function () {
        core.log('自动填入邀请码')
        getInviteCode();
    };

    function getInviteCode() {
        document.getElementById("invite").value = '10XMH';
        /* api.getClipboardData({
            success: function (res) {
                if (res.data.length == 5) {
                    document.getElementById("invite").value = res.data;
                }
            }
        }); */
    }


    var timestart = false;
    var timec = 60;
    async function getCode() {

        var dom = document.querySelector("#agree");
        if (!dom.checked) {
            core.toast('请同意注册协议.');
            return;
        }
        var userphone = $api.trim(document.getElementById("userphone").value);
        userphone = $api.trim(userphone);
        
        if (userphone.length != 11) {
            core.toast("手机号必需是11位.");
            return;
        }

        if (timestart) return;
        timec = 60;
        timestart = true;
        var pcode = Number(userphone) % 2023 + Number(userphone) % 20232023;
        var dt = { userphone, pcode };
        let res = await core.post("user/getCode", dt);
        core.log(res);
        core.toast(res.msg);
    }

    // 注册
    async function fnRegister() {
        var dom = document.querySelector("#agree");
        if (!dom.checked) {
            core.toast('请同意注册协议.');
            return;
        }
        var userphone = $api.trim(document.getElementById("userphone").value);
        var username = $api.trim(document.getElementById("username").value);
        var code = $api.trim(document.getElementById("code").value);
        var password = $api.trim(document.getElementById("password").value);
        var password2 = $api.trim(document.getElementById("password2").value);
        var invite = $api.trim(document.getElementById("invite").value);
        if (userphone.length != 11) {
            core.toast("手机号必需11位.请检查.");
            return;
        }
        if (password.length < 6) {
            core.toast("密码最少6位，请检查.");
            return;
        }
        if (password != password2) {
            core.toast("两次密码输入不相同.");
            return;
        }
        var dt = { userphone,  pass:password, name:username,invite:invite};
        let res = await core.post("api/tuser/reg", dt);
        core.log('注册返回数据')
        core.log(res)
        if (res.status == 1) {
            $api.setStorage('userinfo', res.data);
            api.alert({
                title: '提示',
                msg: '注册成功，已经自动登录.APP将自动重启加载.',
            }, function (ret, err) {
                if (ret) {
                    api.rebootApp();
                }
            });
        } else {
            api.alert({
                title: '注册失败:',
                msg: res.msg,
            }, function (ret, err) {
            });
        }

    }

    function back2() {
        api.execScript({

            name: 'login',
            script: 'toBack(); '
        });
        api.closeWin();
    }
</script>

</html>